import { getAlarmTypeList } from '@/api/alarm/alarm'
import { getAllRoleList, isAccountExist } from '@/api/sys/system'
import { BasicColumn, FormSchema } from '@/components/Table'

export const columns: BasicColumn[] = [
  {
    title: '设备编码',
    dataIndex: 'code',
    customRender: ({ record }) => {
      return record.device.code
    },
  },
  {
    title: '设备名称',
    dataIndex: 'deviceName',
    customRender: ({ record }) => {
      return record.device.name
    },
  },
  {
    title: '告警时间',
    dataIndex: 'alarmTime',
  },
  {
    title: '告警类型',
    dataIndex: 'name',
    customRender: ({ record }) => {
      return record.alarmType.name
    },
  },
  {
    title: '告警参数',
    dataIndex: 'alarmPropertyName',
  },
  {
    title: '告警内容',
    dataIndex: 'alarmContent',
  },
]

export const searchFormSchema: FormSchema[] = [
  {
    field: 'deviceCode',
    label: '设备编码',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    field: 'alarmPropertyName',
    label: '告警参数',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    field: 'alarmTypeId',
    label: '告警类型',
    // component: 'Input',
    colProps: { span: 6 },
    component: 'ApiSelect',
    componentProps: () => {
      return {
        api: getAlarmTypeList,
        labelField: 'name',
        valueField: 'id',
        // mode: 'multiple',
        allowClear: true,
        // maxTagCcount: 3,
        // maxTagTextLength: 10,
        // getPopupContainer: () => {
        //   return document.querySelector('.ant-board')!
        // },
      }
    },
  },
  {
    field: 'time',
    component: 'RangePicker',
    colProps: { span: 12 },
    // label: '起止日期',
  },
  // {
  //   field: 'name',
  //   label: '告警类型',
  //   component: 'Input',
  //   colProps: { span: 6 },
  // },
]

export const accountFormSchema: FormSchema[] = [
  {
    label: '数据分类',
    field: 'type',
    component: 'ApiSelect',
    componentProps: {
      api: getAllRoleList,
      labelField: 'roleName',
      valueField: 'roleValue',
    },
    required: true,
  },
  {
    field: 'name',
    label: '数据名称',
    component: 'Input',
    helpMessage: ['本字段演示异步验证', '不能输入带有admin的用户名'],
    rules: [
      {
        required: true,
        message: '请输入用户名',
      },
      {
        trigger: 'blur',
        validator(_, value) {
          return new Promise((resolve, reject) => {
            if (!value) return resolve()
            isAccountExist(value)
              .then(resolve)
              .catch((err) => {
                reject(err.message || '验证失败')
              })
          })
        },
      },
    ],
  },
  {
    field: 'code',
    label: '设备参数',
    component: 'Input',
    required: true,
    ifShow: false,
  },
  {
    field: 'createtime',
    label: '数据日期',
    component: 'DatePicker',
  },
  {
    field: 'value',
    label: '参数值',
    component: 'InputNumber',
  },
]
